package com.ssm.hxj.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageInfo;
import com.ssm.hxj.page.Page;
import com.ssm.hxj.service.tjscService;
import com.ssm.hxj.view.tjscviewview;
import com.ssm.pojo.Tjsc;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;


@RestController
@RequestMapping("//hxj")
public class tjscController {
    @Resource
    private tjscService tjscService;
    //查询
    @RequestMapping("searchQuerytjsc")
    public PageInfo<Tjsc> searchQuerytjsc(@RequestBody Page<Tjsc> page) throws Exception {
        System.out.println("----------------"+page);
        return this.tjscService.searchQuerytjscService(page);
    }

    @PostMapping("/upload")
    public String upload(MultipartFile file){
// Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId =  "LTAI5t8jnn1AXzNU3j9kJH9K";
        String accessKeySecret = "phXj04J1Qm5h0g2VIiFJA4T9zX627L";
// 填写Bucket名称，例如examplebucket。
        String bucketName = "imgag";
//你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = file.getOriginalFilename();
// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;

    }

    @PostMapping("/scokkk")
    public int scokkk(@RequestBody() tjscviewview tjscviewview){
        this.tjscService.scokkkService(tjscviewview);
        return 1;
    }
}
